/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 23:13:59
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.edu.bean.*;
import com.joysuch.wwyt.edu.bean.EduExamRecordListBean.SearchEduExamRecord;
import com.joysuch.wwyt.edu.service.EduCourseStatisticsService;
import com.joysuch.wwyt.edu.service.EduExamRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/edu/exam-record")
@Api(tags = "/edu/exam-record 考试记录")
public class EduExamRecordController {

    private static final Logger logger = LoggerFactory.getLogger(EduExamRecordController.class);

    @Autowired
    private EduExamRecordService eduExamRecordService;
    @Autowired
    private EduCourseStatisticsService eduCourseStatisticsService;


    @RequiresPermissions("edu:exam:record:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody EduExamRecordEditBean entity) {
        eduExamRecordService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:exam:record:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody EduExamRecordEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        eduExamRecordService.update(entity);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:exam:record")
    @RequestMapping("page")
    @ApiOperation("分页查询")
    public Object page(Pageable page, SearchEduExamRecord condition) {
        Page<EduExamRecordListBean> data = eduExamRecordService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    // @RequiresPermissions("edu:exam:record")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduExamRecord condition) {
        Page<EduExamRecordListBean> data = eduExamRecordService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("edu:exam:record:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        eduExamRecordService.delete(id);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:exam:record:delete")
    @RequestMapping("batch-delete")
    public Object batchDelete(@RequestBody List<Long> ids) throws Exception {
        eduExamRecordService.deleteAll(ids);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:exam:record:delete")
    @RequestMapping("batch-delete-by-user")
    public Object batchDeleteByUser(Long examId, String userIds) throws Exception {
        List<Long> userIdList = CollectionUtils.splitAndConvert(userIds, ",", Long::parseLong);
        eduExamRecordService.batchDeleteByExamIdAndUserId(examId, userIdList);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:exam:record")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(eduExamRecordService.getById(id));
    }

    @GetMapping("get-user-info")
    @ApiOperation(value = "web端个人考试记录基本信息", response = EduExamJoinUserListBean.class)
    public ResultBean getExamRecordUserInfo(Long id) {
        return ResultBean.success(eduCourseStatisticsService.getExamRecordUserInfo(id));
    }

    @GetMapping("web-user-info-exam")
    @ApiOperation(value = "web端个人信息查询-考试记录", response = EduExamUserInfoDto.class)
    public ResultBean userInfoExam(Long userId, Pageable pageable) {
        return eduExamRecordService.userInfoExam(userId, pageable);
    }

    @GetMapping("user-exam-record-answer-list")
    @ApiOperation(value = "web端查看用户答题详情-考试记录", response = EduExamUserAnswerDetailBean.class)
    public ResultBean queryUserExamRecordAnswerList(Long examRecordId) {
        return eduExamRecordService.queryUserExamRecordAnswerList(examRecordId);
    }

    @ApiOperation(value = "导出考试pdf文件")
    @RequestMapping("export/pdf")
    public void  exportPdf(Long examId,String userIds ,HttpServletResponse response) {
        eduExamRecordService.exportPdf(examId,response,userIds);
    }
}
